package civicinity.ist.psu.edu.events;

import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.share.CivicinityMyView;
import civicinity.ist.psu.edu.share.CivicinityShareListview;
import civicinity.ist.psu.edu.utils.CivicinityCameraMode;
import civicinity.ist.psu.edu.utils.CivicinityDrawableManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CivicinityEventsDetail extends Activity {

	private int mEventID;
	private String mEventTitle;
	private String mEventDescription;
	
	private TextView eventTitleTxt;
	private ImageView eventImg;
	private TextView eventDescriptionTxt;
	private Button seePhotoBtn;
	private Button participateBtn;
	private ProgressBar mSpinner;
	
	private String mSelectedImagePath;
	private static final int SELECT_PICTURE = 1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.event_detail);
	    
	    // get the values from previous intent
	    Intent gIntent = getIntent();
	    mEventID = gIntent.getExtras().getInt("event_id");
	    mEventTitle = gIntent.getExtras().getString("event_title");
	    mEventDescription = gIntent.getExtras().getString("event_description");
	    
	    // initialize the layout
	    eventImg = (ImageView) findViewById(R.id.event_image);
	    //eventTitleTxt = (TextView) findViewById(R.id.event_title);
	    eventDescriptionTxt = (TextView) findViewById(R.id.event_description);
	    seePhotoBtn = (Button) findViewById(R.id.see_photos);
	    participateBtn = (Button) findViewById(R.id.participate);
	    mSpinner = (ProgressBar) findViewById(R.id.spinner);
	    
	    // add values into the components
	    //eventTitleTxt.setText(mEventTitle);
	    eventDescriptionTxt.setText(mEventDescription);
	    
	    // load the image on the ImageView
	    CivicinityDrawableManager dm = new CivicinityDrawableManager(this, mSpinner, eventImg);
		dm.fetchDrawableOnThread(Constants.DOWNLOAD_EVENT_IMAGE + mEventID + ".jpg");
	    
		// click listener
	    seePhotoBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CivicinityEventsDetail.this, CivicinityGallery.class);
				intent.putExtra("event_id", mEventID);
				startActivity(intent);
			}
		});
	    
	    participateBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final CharSequence[] choiceSeq = {"Choose from Gallery", "Capture a photo"};
				
				new AlertDialog.Builder(CivicinityEventsDetail.this)
		   		.setTitle("Upload photo")
		   		.setItems(choiceSeq, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which == 0) {
							Intent intent = new Intent();
		                    intent.setType("image/*");
		                    intent.setAction(Intent.ACTION_GET_CONTENT);
		                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
						}
						else {
							Intent intent = new Intent(CivicinityEventsDetail.this, CivicinityCameraMode.class);
							intent.putExtra("prev_activity", Constants.PREV_ACITIVITY_EVENT);
							intent.putExtra("event_id", mEventID);
							startActivity(intent);
						}
					}
				})
		   		.show();
			}
		});
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (resultCode == RESULT_OK) {
	        if (requestCode == SELECT_PICTURE) {
	            Uri selectedImageUri = data.getData();
	            mSelectedImagePath = getPath(selectedImageUri);
	            
	            Intent intent = new Intent(this, CivicinityEventParticipate.class);
	            intent.putExtra("image_path", mSelectedImagePath);
	            intent.putExtra("event_id", mEventID);
	            startActivity(intent);
	            
	            finish();
	        }
	    }
	}
	
	public String getPath(Uri uri) {
	    String[] projection = { MediaStore.Images.Media.DATA };
	    Cursor cursor = managedQuery(uri, projection, null, null, null);
	    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
	    cursor.moveToFirst();
	    return cursor.getString(column_index);
	}

}
